package cn.itsource.hrm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;


@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MyResourceServerConfigurer extends ResourceServerConfigurerAdapter {


    //1.1资源服务令牌验证服务,通过远程校验令牌
//    @Bean
//    public ResourceServerTokenServices resourceServerTokenServices(){
//        //使用远程服务请求授权服务器校验token ， 即：资源服务和授权服务器不在一个主机
//        RemoteTokenServices services = new RemoteTokenServices();
//        //授权服务地址 , 当浏览器访问某个资源时就会调用该远程授权服务地址去校验token
//        //要求请求中必须携带token
//        services.setCheckTokenEndpointUrl("http://localhost:1100/oauth/check_token");
//        //客户端id，对应认证服务的客户端详情配置的clientId
//        services.setClientId("system");
//        //密钥，对应认证服务的客户端详情配置的clientId
//        services.setClientSecret("123");
//        return services;
//    }

    // 权限的校验
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        //我的资源名称是什么
        resources.resourceId("course");
        //用来校验，解析Token的服务
        //resources.tokenServices(resourceServerTokenServices());
        // 修改token的存储方式
        resources.tokenStore(tokenStore());
    }

    //设置JWT签名密钥。它可以是简单的MAC密钥，也可以是RSA密钥
    private final String sign_key  = "123";

    //JWT令牌校验工具
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter(){
        JwtAccessTokenConverter jwtAccessTokenConverter =
                new JwtAccessTokenConverter();
        //设置JWT签名密钥。它可以是简单的MAC密钥，也可以是RSA密钥
        jwtAccessTokenConverter.setSigningKey(sign_key);
        return jwtAccessTokenConverter;
    }

    @Bean
    public TokenStore tokenStore(){

        // return new InMemoryTokenStore(); 存在内存种的
        // 提供JWM的token转换器
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    // 权限路径的匹配验证
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                //校验scope必须为all ， 对应认证服务的客户端详情配置的clientId
                .antMatchers("/**").access("#oauth2.hasScope('hrm')")
                //关闭跨域伪造检查
                .and().csrf().disable()
                //把session设置为无状态，意思是使用了token，那么session不再做数据的记录
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }
}
